{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7c5a114d",
   "metadata": {},
   "source": [
    "## 1.语音录制\n",
    "\n",
    "* 1.安装和下载模块：pip install 模块名称\n",
    "> 1. speech_recognition\n",
    "> 2. Pyaudio  \n",
    "> 3. pydub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "891b31bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e22c9459",
   "metadata": {},
   "source": [
    "## 2.语音识别（百度的ASR/ auto speech recognition）\n",
    "\n",
    "* I/O: 音频文件 ——> 文本文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "19fea861",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "76b0af83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 1.022630\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'嗯嗯嗯嗯嗯。'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bdasr_result  = eval(bdasr.asr('1.wav'))['result'][0]\n",
    "bdasr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ee1eb44",
   "metadata": {},
   "source": [
    "## 3.自然语言处理（NLP）\n",
    "\n",
    "* 文本文件 ——> 文本文件（文本的生成：文本的理解，文本的处理，文本的生成）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c43fe838",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"你是谁？\":\"我是集美貌和智慧于一体的智能小网同学\",\n",
    "    '你好。':\"你也好，今天听起来心情不错哦\",\n",
    "    '北京科技馆。':\"中国科学技术馆，位于北京市朝阳区北辰东路5号，东临亚运居住区，西濒奥运水系，南依奥运主体育场，北望森林公园，于1958年筹建，建设分为两期工程和新馆工程，分别于1988年9月22日、2000年4月29日、2009年9月16日开放。\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0866a467",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa_result = qa.get(bdasr_result)\n",
    "qa_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5456363",
   "metadata": {},
   "source": [
    "## 4.语音合成（百度TTS）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4e0de306",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUCCESS WITH TOKEN: 24.c616886d3836e8c9bfec4e78690efcc4.2592000.1687091317.282335-19331335 ; EXPIRES IN SECONDS: 2592000\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "quote_from_bytes() expected bytes",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [10]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mbdtts\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mbdtts\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtts\u001b[49m\u001b[43m(\u001b[49m\u001b[43mqa_result\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32m~\\Desktop\\API\\week13\\bdtts.py:89\u001b[0m, in \u001b[0;36mtts\u001b[1;34m(TEXT)\u001b[0m\n\u001b[0;32m     87\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtts\u001b[39m(TEXT):\n\u001b[0;32m     88\u001b[0m     token \u001b[38;5;241m=\u001b[39m fetch_token()\n\u001b[1;32m---> 89\u001b[0m     tex \u001b[38;5;241m=\u001b[39m \u001b[43mquote_plus\u001b[49m\u001b[43m(\u001b[49m\u001b[43mTEXT\u001b[49m\u001b[43m)\u001b[49m  \u001b[38;5;66;03m# 此处TEXT需要两次urlencode\u001b[39;00m\n\u001b[0;32m     90\u001b[0m     \u001b[38;5;66;03m# print(tex)\u001b[39;00m\n\u001b[0;32m     91\u001b[0m     params \u001b[38;5;241m=\u001b[39m {\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtok\u001b[39m\u001b[38;5;124m'\u001b[39m: token, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtex\u001b[39m\u001b[38;5;124m'\u001b[39m: tex, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mper\u001b[39m\u001b[38;5;124m'\u001b[39m: PER, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mspd\u001b[39m\u001b[38;5;124m'\u001b[39m: SPD, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpit\u001b[39m\u001b[38;5;124m'\u001b[39m: PIT, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvol\u001b[39m\u001b[38;5;124m'\u001b[39m: VOL, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124maue\u001b[39m\u001b[38;5;124m'\u001b[39m: AUE, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mcuid\u001b[39m\u001b[38;5;124m'\u001b[39m: CUID,\n\u001b[0;32m     92\u001b[0m               \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlan\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mzh\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mctp\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m1\u001b[39m}  \u001b[38;5;66;03m# lan ctp 固定参数\u001b[39;00m\n",
      "File \u001b[1;32m~\\anaconda3\\lib\\urllib\\parse.py:887\u001b[0m, in \u001b[0;36mquote_plus\u001b[1;34m(string, safe, encoding, errors)\u001b[0m\n\u001b[0;32m    885\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    886\u001b[0m     space \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mb\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m--> 887\u001b[0m string \u001b[38;5;241m=\u001b[39m \u001b[43mquote\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstring\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msafe\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mspace\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mencoding\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43merrors\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    888\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m string\u001b[38;5;241m.\u001b[39mreplace(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m+\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "File \u001b[1;32m~\\anaconda3\\lib\\urllib\\parse.py:871\u001b[0m, in \u001b[0;36mquote\u001b[1;34m(string, safe, encoding, errors)\u001b[0m\n\u001b[0;32m    869\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m errors \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m    870\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mquote() doesn\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt support \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124merrors\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m for bytes\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m--> 871\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mquote_from_bytes\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstring\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msafe\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32m~\\anaconda3\\lib\\urllib\\parse.py:896\u001b[0m, in \u001b[0;36mquote_from_bytes\u001b[1;34m(bs, safe)\u001b[0m\n\u001b[0;32m    891\u001b[0m \u001b[38;5;124;03m\"\"\"Like quote(), but accepts a bytes object rather than a str, and does\u001b[39;00m\n\u001b[0;32m    892\u001b[0m \u001b[38;5;124;03mnot perform string-to-bytes encoding.  It always returns an ASCII string.\u001b[39;00m\n\u001b[0;32m    893\u001b[0m \u001b[38;5;124;03mquote_from_bytes(b'abc def\\x3f') -> 'abc%20def%3f'\u001b[39;00m\n\u001b[0;32m    894\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    895\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(bs, (\u001b[38;5;28mbytes\u001b[39m, \u001b[38;5;28mbytearray\u001b[39m)):\n\u001b[1;32m--> 896\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mquote_from_bytes() expected bytes\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    897\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m bs:\n\u001b[0;32m    898\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m\n",
      "\u001b[1;31mTypeError\u001b[0m: quote_from_bytes() expected bytes"
     ]
    }
   ],
   "source": [
    "import bdtts\n",
    "bdtts.tts(qa_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d7de541",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09c917b8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "224071bf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
